//
//  BGMDateView.m
//  BGM
//
//  Created by 我要变好看 on 2018/12/23.
//  Copyright © 2018 com.alirtc.demo. All rights reserved.
//

#import "BGMDateView.h"
#import "Common.h"
#import <objc/runtime.h>


@implementation BGMDateView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self != nil) {
        
        self.backgroundColor = [UIColor clearColor];
        
        
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREENH-250, SCREENW, 250)];
        bgView.backgroundColor = bgmColor(244, 244, 244);
        [self addSubview:bgView];
        
        
        UIButton *btn9 = [[UIButton alloc]initWithFrame:CGRectMake(SCREENW-70, 0, 50, 50)];
        [btn9 setTitle:@"完成" forState:UIControlStateNormal];
        [btn9 setTitleColor:bgmmainColor forState:UIControlStateNormal];
        btn9.titleLabel.font = [UIFont systemFontOfSize:15];
        btn9.tag = 99;
        [bgView addSubview:btn9];
        
        UIButton *btn10 = [[UIButton alloc]initWithFrame:CGRectMake(20, 0, 50, 50)];
        [btn10 setTitle:@"取消" forState:UIControlStateNormal];
        [btn10 setTitleColor:bgmmainColor forState:UIControlStateNormal];
        btn10.titleLabel.font = [UIFont systemFontOfSize:15];
        btn10.tag = 100;
        [bgView addSubview:btn10];
        [btn9 addTarget:self action:@selector(clickData:) forControlEvents:UIControlEventTouchUpInside];
        [btn10 addTarget:self action:@selector(clickData:) forControlEvents:UIControlEventTouchUpInside];
        
        
        UIDatePicker *datePicker = [ [ UIDatePicker alloc] initWithFrame:CGRectMake(0,50,SCREENW,200)];
        datePicker.backgroundColor = [UIColor whiteColor];
        datePicker.datePickerMode = UIDatePickerModeDate;
        datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
        [datePicker setMaximumDate: [NSDate new]];
        [datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
        
        unsigned int outCount;
        int i;
        objc_property_t *pProperty = class_copyPropertyList([UIDatePicker class], &outCount);
        for (i = outCount -1; i >= 0; i--){
             NSString *getPropertyName = [NSString stringWithCString:property_getName(pProperty[i]) encoding:NSUTF8StringEncoding];
            if([getPropertyName isEqualToString:@"textColor"]){
                [datePicker setValue:bgmyelloeColor forKey:@"textColor"];
            }
        }
        [bgView addSubview:datePicker];
        
        
    }
    return self;
}
- (void)clickData:(UIButton *)btn{
    
    int tag = (int)btn.tag;
    if (tag == 99) {
        [self.userDateDelegate saveBirth:self.birth];
    }else{
        [self.userDateDelegate hiddenBirthView];
    }
    
}

-(void)dateChange:(UIDatePicker *)datePicker{
    NSDate *theDate = datePicker.date;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"YYYY-MM-dd";
    self.birth = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:theDate]];
 }
 

@end
